package com.zyl.pagergalleryloadmoredemo.adapter

import android.graphics.drawable.Drawable
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.navigation.findNavController
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.zyl.pagergalleryloadmoredemo.R
import com.zyl.pagergalleryloadmoredemo.pojo.PhotoItem
import com.zyl.pagergalleryloadmoredemo.viewModel.DATA_STATUS_CAN_LOAD_MORE
import com.zyl.pagergalleryloadmoredemo.viewModel.DATA_STATUS_NETWORK_ERROR
import com.zyl.pagergalleryloadmoredemo.viewModel.DATA_STATUS_NO_MORE
import com.zyl.pagergalleryloadmoredemo.viewModel.GalleryViewModel
import com.zyl.pagergalleryloadmoredemo.volley.VolleyUtils
import kotlinx.android.synthetic.main.cell_gallery.view.*
import kotlinx.android.synthetic.main.gallery_footer.view.*

const val PHOTO_LIST = "photo_list"
const val PHOTO_POSITION = "photo_position"

class GalleryAdapter(var galleryViewModel: GalleryViewModel) :
    ListAdapter<PhotoItem, GalleryViewHolder>(diffCallback) {

    var footerViewStatus: Int = 0

    companion object {
        const val NORMAL_VIEW_TYPE = 0
        const val FOOTER_VIEW_TYPE = 1
    }

    object diffCallback : DiffUtil.ItemCallback<PhotoItem>() {
        override fun areItemsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem.photoId == newItem.photoId
        }

        override fun areContentsTheSame(oldItem: PhotoItem, newItem: PhotoItem): Boolean {
            return oldItem == newItem
        }
    }

    override fun getItemCount(): Int {
        return super.getItemCount() + 1
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == itemCount - 1) FOOTER_VIEW_TYPE else NORMAL_VIEW_TYPE
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GalleryViewHolder {


        val galleryViewHolder: GalleryViewHolder
        if (viewType == NORMAL_VIEW_TYPE) {
            galleryViewHolder = GalleryViewHolder(
                LayoutInflater.from(parent.context).inflate(R.layout.cell_gallery, parent, false)
            )

            galleryViewHolder.itemView.setOnClickListener {
                Bundle().apply {
                    putParcelableArrayList(PHOTO_LIST, ArrayList(currentList))
                    putInt(PHOTO_POSITION, galleryViewHolder.adapterPosition)
                    galleryViewHolder.itemView.findNavController()
                        .navigate(R.id.action_galleryFragment_to_photoFragment, this)
                }
            }
        } else {
            galleryViewHolder = GalleryViewHolder(
                LayoutInflater.from(parent.context).inflate(R.layout.gallery_footer, parent, false)
                    .also {
                        (it.layoutParams as StaggeredGridLayoutManager.LayoutParams).isFullSpan =
                            true //使加载的地方占满
                        it.setOnClickListener { itemView ->
                            itemView.progressBar.visibility = View.VISIBLE
                            itemView.textViewStauts.text = "正在加载"
                            galleryViewModel.fetchData() //点击刷新
                        }
                    }
            )
        }



        return galleryViewHolder
    }

    override fun onBindViewHolder(holder: GalleryViewHolder, position: Int) {
        //如果是页脚下面就不用加载了
        if (position == itemCount - 1) {
            with(holder.itemView) {
                when (footerViewStatus) {
                    DATA_STATUS_CAN_LOAD_MORE -> {
                        progressBar.visibility = View.VISIBLE
                        textViewStauts.text = "正在加载"
                        isClickable = false //不可以点击
                    }

                    DATA_STATUS_NETWORK_ERROR -> {
                        progressBar.visibility = View.GONE
                        textViewStauts.text = "网络错误，点击重试"
                        isClickable = true //可以点击
                    }

                    DATA_STATUS_NO_MORE -> {
                        progressBar.visibility = View.GONE
                        textViewStauts.text = "全部加载完毕"
                        isClickable = false //不可以点击
                    }
                }
            }
            return
        }

        val photoItem = getItem(position)
        holder.itemView.apply {
            shimmerLayout.setShimmerColor(0x55ffffff)
            shimmerLayout.setShimmerAngle(30)
            shimmerLayout.startShimmerAnimation()
            tv_user.text = photoItem.photoUser
            tv_favorites.text = photoItem.photoFavorites.toString()
            tv_likes.text = photoItem.photoLikes.toString()
            imageViewGallery.layoutParams.height = photoItem.photoHeight
        }

        Glide.with(holder.itemView)
            .load(photoItem.previewUrl)
            .placeholder(R.drawable.ic_baseline_photo_24)
            .listener(object : RequestListener<Drawable> {
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Drawable>?,
                    isFirstResource: Boolean
                ): Boolean {
                    return false
                }

                override fun onResourceReady(
                    resource: Drawable?,
                    model: Any?,
                    target: Target<Drawable>?,
                    dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {
                    return false.also { holder.itemView.shimmerLayout.stopShimmerAnimation() }
                }
            })
            .into(holder.itemView.imageViewGallery)

    }
}

class GalleryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)